{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 阶乘求和\n",
    "# 输入一个正整数n，计算1!+2!+3!+...+n!的值。\n",
    "n = int(input())\n",
    "def factorial_sum(n):\n",
    "    result = 0\n",
    "    factorial = 1 # 由于n为正整数，初始化阶乘为1\n",
    "    for i in range(1, n + 1):\n",
    "        factorial *= i  # 计算阶乘\n",
    "        result += factorial\n",
    "    return result\n",
    "print(factorial_sum(n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "和为： 7407407400\n"
     ]
    }
   ],
   "source": [
    "# 2. 用for和while语句分别求和6+66+……+6666666666\n",
    "# for\n",
    "sum_for = 0\n",
    "for i in range(1, 11):\n",
    "    sum_for += int('6' * i)  # 采用str乘法，再转换为int\n",
    "\n",
    "# while\n",
    "sum_while = 0\n",
    "i = 1\n",
    "while i <= 10:\n",
    "    sum_while += int('6' * i)\n",
    "    i += 1\n",
    "print(\"和为：\", sum_while)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4. 判断素数\n",
    "def is_prime(num):\n",
    "    if num <= 1: # 0和1不是素数\n",
    "        return False\n",
    "    for i in range(2, num):\n",
    "        if num % i == 0: # 如果能被(2, n-1)的数整除，则不是素数\n",
    "            return False\n",
    "    return True\n",
    "N = int(input())\n",
    "print(is_prime(N))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 是素数\n"
     ]
    }
   ],
   "source": [
    "# 判断素数\n",
    "# while i < n:\n",
    "#     if n % i == 0:\n",
    "#         print(n, \"不是素数\")\n",
    "#         break\n",
    "#     i += 1\n",
    "# if i == n:  # 如果循环结束时i等于n，说明没有找到能整除n的数\n",
    "#     print(n, \"是素数\")\n",
    "# else:\n",
    "#     print(n, \"不是素数\")\n",
    "\n",
    "# for循环，这个写法和上面的is_prime()逻辑相同\n",
    "n = 3\n",
    "for i in range(2, n):\n",
    "    if n % i == 0:\n",
    "        print(n, \"不是素数\")\n",
    "        break\n",
    "else:\n",
    "    print(n, \"是素数\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里的 else 是和 for 循环配对的，而不是和 if。它的结构是：\n",
    "\n",
    "如果 for 循环没有被 break 语句中断，循环正常结束，则执行 else 语句块。\n",
    "\n",
    "如果循环中遇到 break，则不会执行 else 语句块。\n",
    "\n",
    "所以，else 必须和 for 对齐，而不是和 if 对齐。这是 Python 的语法特性。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5. 数列求和\n",
    "# 计算1-1/2+1/3-1/4+1/5-1/6+...+1/n的值\n",
    "n = int(input())\n",
    "def calculate(n):\n",
    "    if n == 1:\n",
    "        return 1\n",
    "    if n % 2 == 0: # 偶数- ，奇数+\n",
    "        return(calculate(n - 1) - 1 / n)\n",
    "    else:\n",
    "        return(calculate(n - 1) + 1 / n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 6. 输入一个非 0 十进制整数(不允许前导0的存在，即不允许类似 0123 这样的输入)，将其反转输出。\n",
    "num = input()\n",
    "if num[0].isdigit():  # 检查\n",
    "    reversed_num = num[::-1].strip('0')  # 去掉首位的0\n",
    "    print(reversed_num)\n",
    "else:\n",
    "    num_tmp = num[1:]  # 去掉符号\n",
    "    if num_tmp[0].isdigit():  # 检查\n",
    "        reversed_num = num_tmp[::-1].strip('0')  # 去掉首尾的0\n",
    "        print(num[0] + reversed_num)  # 输出再加上符号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 7. 获得输入正整数 N，计算各位数字的平方和，直接输出结果。本题不考虑输入异常情况。\n",
    "n = int(input())\n",
    "def square_sum(n):\n",
    "    result = 0\n",
    "    while n > 0:\n",
    "        digit = n % 10  # 循环获取最后一位数字\n",
    "        result += digit ** 2\n",
    "        n //= 10  # 去掉当前数字的个位\n",
    "    return result\n",
    "print(square_sum(n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 "
     ]
    }
   ],
   "source": [
    "# 8. 输出100以内的所有素数\n",
    "def is_prime(num):\n",
    "    if num <= 1: # 0和1不是素数\n",
    "        return False\n",
    "    for i in range(2, num):\n",
    "        if num % i == 0: # 如果能被(2, n-1)的数整除，则不是素数\n",
    "            return False\n",
    "    return True\n",
    "for i in range(2, 101):\n",
    "    if is_prime(i):\n",
    "        print(i, end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# x + y + z = 100\n",
    "# 5x + 3y + z/3 = 100\n",
    "\n",
    "# 公鸡最多20个\n",
    "for x in range(0, 21):\n",
    "    # 母鸡最多33个\n",
    "    for y in range(0, 34):\n",
    "        # 小鸡\n",
    "        z = 100 - x - y\n",
    "        if 5 * x + 3 * y + z / 3 == 100:\n",
    "            print(f\"公鸡:{x}  母鸡:{y}  小鸡:{z}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    *\n",
      "   ***\n",
      "  *****\n",
      " *******\n",
      "*********\n",
      " *******\n",
      "  *****\n",
      "   ***\n",
      "    *\n"
     ]
    }
   ],
   "source": [
    "# 打印星号三角形金字塔\n",
    "for i in range(1, 6):\n",
    "    print(' ' * (5 - i) + '*' * (2 * i - 1))\n",
    "\n",
    "# 打印倒三角形金字塔\n",
    "for i in range(4, 0, -1):\n",
    "    print(' ' * (5 - i) + '*' * (2 * i - 1))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
